package com.suxii.frpclient.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.suxii.frpclient.MainActivity;
import com.suxii.frpclient.R;
import com.suxii.frpclient.databinding.FragmentHomeBinding;
import com.suxii.frpclient.databinding.FragmentSettingBinding;
import com.suxii.frpclient.message.ConfigTextMessage;
import com.suxii.frpclient.message.TaskRunningMessage;
import com.suxii.frpclient.service.FrpService;
import com.suxii.frpclient.utils.FileUtil;
import com.suxii.frpclient.utils.RxBus;

import java.io.IOException;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

public class SettingFragment extends Fragment {

    public static final String NAME = "SETTING";


    private final CompositeDisposable compositeDisposable = new CompositeDisposable();
    private FragmentSettingBinding binding;

    public static SettingFragment newInstance() {
        SettingFragment fragment = new SettingFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentSettingBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        initEvent();
        return view;
    }

    private void initEvent() {
        // 订阅 RxBus 中的数据
        Disposable disposable = RxBus.getInstance().toObservable(ConfigTextMessage.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(data -> {
                    Log.e("aa","setting"+"\n"+data.getData());
                    binding.configText.setText(data.getData());
                });

        binding.saveBtn.setOnClickListener(v -> {
            String text = binding.configText.getText().toString();
            FileUtil.saveConfigFile(requireContext(),text);
            Toast.makeText(requireContext(), "修改成功", Toast.LENGTH_SHORT).show();
        });
        binding.defaultBtn.setOnClickListener(v -> {
            FileUtil.initConfigFile(requireContext());
            Toast.makeText(requireContext(), "已恢复默认", Toast.LENGTH_SHORT).show();
            initConfigText();
        });


        compositeDisposable.add(disposable);
    }

    private void initConfigText() {
        try {
            String text = FileUtil.getConfigContext(requireContext());
            RxBus.getInstance().post(new ConfigTextMessage(text));
        } catch (IOException e) {
            Log.e("aa","没有配置文件");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        initConfigText();
    }

    @Override
    public void onDestroy() {
        compositeDisposable.clear();
        super.onDestroy();
    }
}